package com.rongshu.sendmessage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.bean.Comments;
import com.rongshu.sendmessage.bean.PingLun;
import com.rongshu.sendmessage.util.IOnItemClickListener;

import java.text.BreakIterator;
import java.util.List;

/**
 * 评论列表item
 */
public class MessagePingLunAdapter extends RecyclerView.Adapter<MessagePingLunAdapter.MyViewHoler> implements View.OnClickListener {
    private final IOnItemClickListener listener;
    private int ResourceID;
    private Context mContext;
    private List<Comments> mList;
    private ImageLoader imageLoader;
    private ImageDataAdapter baseDataAdapterMain;

    public List<Comments> getmList() {
        return mList;
    }

    public void setmList(List<Comments> mList) {
        this.mList = mList;
    }

    public MessagePingLunAdapter(Context context, int resourceID, List<Comments> list, IOnItemClickListener listener) {
        mContext = context;
        mList = list;
        ResourceID = resourceID;
        this.listener = listener;

    }
    @Override
    public void onBindViewHolder(MyViewHoler holder, int position) {
        //相当于listview的adapter中的getview方法
        //负责将数据绑定到视图上
        if (imageLoader == null)
            imageLoader = ImageLoader.getInstance();
        Comments messageList = mList.get(position);
        holder.tv_title.setText(messageList.getCommentUserNick());
        DraweeController controller= Fresco.newDraweeControllerBuilder()
                .setUri(messageList.getCommentUserAvatarUrl())
                .setOldController(holder.iv_head_image.getController())
                .build();
        holder.iv_head_image.setController(controller);
        holder.tv_content.setText(messageList.getCommentContent());
        holder.tv_time.setText(messageList.getCommentDate());

        holder.ll_item.setTag(position);//将位置保存在tag中
        holder.ll_item.setOnClickListener(this);
    }
    @Override
    public MyViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        //负责创建视图
        View view = LayoutInflater.from(mContext).inflate(ResourceID, null);
        return new MyViewHoler(view);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClick(v, (int) v.getTag());
        }
    }

    public interface OnRecycleViewItemClickListener {
        void OnItemClick(View view, int position);
    }

    class MyViewHoler extends RecyclerView.ViewHolder {

        private final SimpleDraweeView iv_head_image;
        private final TextView tv_title,tv_content,tv_time;
        public View ll_item;

        public MyViewHoler(View itemView) {
            super(itemView);
            iv_head_image=itemView.findViewById(R.id.iv_head_image);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_content=itemView.findViewById(R.id.tv_content);
            tv_time=itemView.findViewById(R.id.tv_time);
            ll_item=itemView.findViewById(R.id.ll_item);
        }
    }
}
